package com.ptu.spzx.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.ptu.spzx.domain.Delete.DeleteIds;
import com.ptu.spzx.domain.Question.Dto.QuestionDTO;
import com.ptu.spzx.domain.Question.Dto.QuestionPageDto;
import com.ptu.spzx.domain.Question.Vo.QuestionVO;
import com.ptu.spzx.domain.Question.po.Question;
import com.ptu.spzx.model.vo.common.Result;
import com.ptu.spzx.model.vo.common.ResultCodeEnum;
import com.ptu.spzx.service.IQuestionService;
import lombok.RequiredArgsConstructor;
import org.simpleframework.xml.Path;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author YT
 * @since 2024-01-28
 */
@RestController
@RequestMapping("/sc/exam/qbank")
@RequiredArgsConstructor
public class QuestionController {
    private final IQuestionService iQuestionService;

    //添加题目
    @PostMapping("/addBank")
    @PreAuthorize("@permissionService.hasPermission('exam:qbank:addBank')")
    public Result addQuestion(@RequestBody QuestionDTO questionDTO) {
        iQuestionService.addQuestion(questionDTO);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    //查询题目根据id
    @GetMapping("/queryBank/{id}")
    @PreAuthorize("@permissionService.hasPermission('exam:qbank:queryBank')")
    public Result<QuestionVO> queryQuestion(@PathVariable("id") Long id) {
        QuestionVO question = iQuestionService.queryQuestion(id);
        return Result.build(question, ResultCodeEnum.SUCCESS);
    }

    //查询题目根据id和类型
    @GetMapping("/queryBankList")
    @PreAuthorize("@permissionService.hasPermission('exam:qbank:queryBankList')")
    public Result queryQuestionList(@RequestParam Long courseId,
                                                      @RequestParam(required = false) Long questionType,
                                                      @RequestParam Integer pageNum,
                                                      @RequestParam Integer pageSize,
                                                      @RequestParam String keyWord) {
        PageInfo<Question> questions = iQuestionService.queryQuestionList(courseId, questionType, pageNum, pageSize,keyWord);
        return Result.build(questions, ResultCodeEnum.SUCCESS);
    }

    //更新题目
    @PostMapping("/updateBank")
    @PreAuthorize("@permissionService.hasPermission('exam:qbank:updateBank')")
    public Result updateQuestion(@RequestBody QuestionDTO questionDTO) {
        iQuestionService.updateQuestion(questionDTO);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    //批量删除题目
    @PostMapping("/deleteBatchBank")
    @PreAuthorize("@permissionService.hasPermission('exam:qbank:deleteBatchBank')")
    public Result delBatchQuestion(@RequestBody DeleteIds deleteIds){
        iQuestionService.delBatchQuestion(deleteIds);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


}

